<html>
<head><meta charset="utf-8"><title>Why are impl blocks numbered? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20are.20impl.20blocks.20numbered.3F.html">Why are impl blocks numbered?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="228349476"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20are%20impl%20blocks%20numbered%3F/near/228349476" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20are.20impl.20blocks.20numbered.3F.html#228349476">(Mar 02 2021 at 03:59)</a>:</h4>
<p>In various places like the rust demangling convention, I see that individual impl blocks are numbered, meaning that</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">impl</span><span class="w"> </span><span class="n">Foo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="k">fn</span> <span class="nf">foo</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">..</span><span class="p">.</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="w">  </span><span class="k">fn</span> <span class="nf">bar</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">..</span><span class="p">.</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>is treated differently from:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">impl</span><span class="w"> </span><span class="n">Foo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="k">fn</span> <span class="nf">foo</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">..</span><span class="p">.</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="k">impl</span><span class="w"> </span><span class="n">Foo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="k">fn</span> <span class="nf">bar</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">..</span><span class="p">.</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>But as a rust programmer I generally don't pay any attention to whether impl blocks are monolithic or split, and generally write whatever makes sense with the function order wrt other things (like struct definitions). Is there an actual semantic difference here? If yes, when should I avoid splitting impl blocks, and if no, then why is the compiler tracking which impl block a given function is in?</p>



<a name="228349750"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20are%20impl%20blocks%20numbered%3F/near/228349750" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20are.20impl.20blocks.20numbered.3F.html#228349750">(Mar 02 2021 at 04:02)</a>:</h4>
<blockquote>
<p>why is the compiler tracking which impl block a given function is in?</p>
</blockquote>
<p>Maybe to make tracking in-scope generics easier since generics are scoped to an <code>impl</code>?</p>



<a name="228349808"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20are%20impl%20blocks%20numbered%3F/near/228349808" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20are.20impl.20blocks.20numbered.3F.html#228349808">(Mar 02 2021 at 04:03)</a>:</h4>
<p>I guess that makes sense in the front end, but that doesn't explain how this distinction makes its way all the way to codegen</p>



<a name="228355671"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20are%20impl%20blocks%20numbered%3F/near/228355671" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20are.20impl.20blocks.20numbered.3F.html#228355671">(Mar 02 2021 at 05:24)</a>:</h4>
<p>Does debug info have a reference to the block defining the method?</p>



<a name="228357759"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20are%20impl%20blocks%20numbered%3F/near/228357759" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20are.20impl.20blocks.20numbered.3F.html#228357759">(Mar 02 2021 at 05:54)</a>:</h4>
<p>Another example where I think this matters is lang items, since there are some lang items that are impl blocks so they cannot be split</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>